package hevs.eServices.server;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;

import hevs.eServices.client.E_servicesService;

import com.google.gwt.user.server.rpc.RemoteServiceServlet;

/**
 * The server side implementation of the RPC service.
 */
@SuppressWarnings("serial")
public class E_servicesServiceImpl extends RemoteServiceServlet implements
		E_servicesService {

	@Override
	public String sayHello() throws IllegalArgumentException {
		// TODO Auto-generated method stub
		return "hello2";
	}

	@Override
	public String eService(String input) throws IllegalArgumentException {
		// TODO Auto-generated method stub
		return "plouc";
	}

	@Override
	public Date addDay(Date date) throws IllegalArgumentException {
		// TODO Auto-generated method stub
	
		   SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM/dd");
		   SimpleDateFormat monthF = new SimpleDateFormat("MM");
		   SimpleDateFormat yearsF = new SimpleDateFormat("yyyy");
		   SimpleDateFormat daysF = new SimpleDateFormat("dd");
		   
		   int month = Integer.parseInt(monthF.format(date));
		   int year = Integer.parseInt(yearsF.format(date));
		   int day = Integer.parseInt(daysF.format(date));
	
		    
			Calendar c = Calendar.getInstance();
			c.set(year, month, day);
			
			
			
			c.add(Calendar.DATE, 1);
			
			String dateString = c.get(Calendar.YEAR)+"/"+c.get(Calendar.MONTH)+"/"+c.get(Calendar.DAY_OF_MONTH);
			
			Date d = null;
			try {
				d = (Date) formatter.parse(dateString);
			} catch (ParseException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			
		//	System.out.println(c.toString());
			
			return d;
	}

	@Override
	public ArrayList<Date> addDays(Date date, int days)
			throws IllegalArgumentException {
		// TODO Auto-generated method stub
		
		ArrayList<Date> dates = new ArrayList<Date>();
		
		Date temp = date;
		
		for(int i = 0; i <=days; i++){

			//System.out.println(temp.toString());
			dates.add(temp);
			temp = addDay(temp);
		}
		
		return dates;
	}

	@Override
	public Date createDate(String date) throws IllegalArgumentException {
		// TODO Auto-generated method stub
		Date d = null;
		SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
		try {
			d = (Date) formatter.parse(date);
		} catch (ParseException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		return d;
	}


	
}
